#导包
import unittest
import requests
from api.login import LoginApi
from parameterized import parameterized
import json

def build_data():
    json_file = "../data/login.json"
    test_data = []
    with open(json_file,encoding="utf-8") as f:
        json_data = json.load(f)
        for case_data in json_data:
            username = case_data.get("username")
            password = case_data.get("password")
            verify_code = case_data.get("verify_code")
            status_code = case_data.get("status_code")
            status = case_data.get("status")
            msg = case_data.get("msg")
            test_data.append((username,password,verify_code,status_code,status,msg))
        print(test_data)
        return test_data

print(build_data())

#定义测试类
class TestLoginApil(unittest.TestCase):
    #前置处理
    def setUp(self):
        self.login_api = LoginApi()
        self.session = requests.Session()

    #后置处理
    def tearDown(self):
        self.session.close()

    #定义测试方法
    #登录成功
    @parameterized.expand(build_data())
    def test01_login(self,username,password,verify_code,status_code,status,msg):
        # 调用验证码接口并且断言
        response = self.login_api.get_verity_code(self.session)
        self.assertEqual(200,response.status_code)
        self.assertIn("image",response.headers.get("Content-Type"))
        # 调用登录接口并且断言
        response = self.login_api.login(self.session, username, password, verify_code)
        print(response.json())
        self.assertEqual(status_code, response.status_code)
        self.assertEqual(status, response.json().get("status"))
        self.assertIn(msg, response.json().get("msg"))

